/**
 * @file setting.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SETTING != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/
#if USE_LV_WLAN != 0
static uint8_t mobile_data_switch;
#endif

const struct {
    void * img_src;
    watchLangTxtId_t txtId;
    lv_event_cb_t event_cb;
} settingList[] = {
#if USE_LV_WATCH_FLYING_MODE != 0
    { ICON_SETTING_FLYING_MODE, WATCH_TEXT_ID_FLYING_MODE, flying_mode_create_btn_action },
#endif
#if USE_LV_BLUETOOTH != 0
    { ICON_SETTING_BLUETOOTH, WATCH_TEXT_ID_BLUETOOTH, bluetooth_create_btn_action},
#endif
#if USE_LV_WLAN != 0
    { ICON_SETTING_WLAN, WATCH_TEXT_ID_WLAN, wlan_create_btn_action},
#endif
    { ICON_SETTING_DAIL_FORMAT_SWITCH, WATCH_TEXT_ID_DAIL_REPLACE, dial_switch_create_btn_action },
#if USE_LV_WATCH_DIAL_AMBIENT != 0
    { ICON_SETTING_DAIL_PSM_SWITCH, WATCH_TEXT_ID_PSM_DIAL_REPLACE, dial_switch_create_btn_action },
#endif
#if USE_LV_WATCH_LAUNCHER2 != 0
    {ICON_SETTING_TAB, WATCH_TEXT_ID_SETTING_TAB, setting_tab_create_btn_action },
#endif
    { ICON_SETTING_VOLUME, WATCH_TEXT_ID_VOLUME_ADJUST, setting_volume_create_btn_action },
    { ICON_SETTING_BRIGHTNESS, WATCH_TEXT_ID_BRIGHTNESS_ADJUST, setting_brightness_create_btn_action },
    { ICON_SETTING_DATETIME, WATCH_TEXT_ID_DATE_AND_TIME,  setting_date_time_create_btn_action},
    { ICON_SETTING_LANGUAGE, WATCH_TEXT_ID_LANGUAGE_SWITCH, language_create_btn_action },
#if USE_LV_WATCH_VOLTE_SWTICH != 0
    { ICON_SETTING_VOLTE, WATCH_TEXT_ID_VOLTE, volte_switch_create_btn_action },
#endif
#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
    { ICON_SETTING_SD_AUTO_MOUNT, WATCH_TEXT_ID_SD_AUTO_MOUNT, sd_auto_mount_create_btn_action },
#endif
    { ICON_SETTING_RESET, WATCH_TEXT_ID_RESTART_AND_RESET, restart_and_reset_create_btn_action },
#if USE_LV_WATCH_SHUTDOWN != 0
    { ICON_SETTING_SHUTDOWN, WATCH_TEXT_ID_SHUTDOWN, shutdown_create_btn_action },
#endif

#if USE_LV_WATCH_UPGRADE != 0
    { ICON_SETTING_UPGRADE, WATCH_TEXT_ID_SYSTEM_UPGRADE, upgrade_create_btn_action },
#else
    { ICON_SETTING_UPGRADE, WATCH_TEXT_ID_SYSTEM_UPGRADE, btn_event_cb },
#endif
    { ICON_SETTING_ABOUT, WATCH_TEXT_ID_ABOUT_WATCH, setting_about_create_btn_action },
#if USE_LV_WATCH_QRCODE != 0
    { ICON_SETTING_QRCODE, WATCH_TEXT_ID_CREATE_QRCODE, qrcode_create_event_cb },
#endif
    //{ &icon_setting_time_zone, WATCH_TEXT_ID_TIME_ZONE, btn_action },
    //{ &icon_setting_add_friends, WATCH_TEXT_ID_ADD_FRIEND_ADJUST, btn_action },
    //{ &icon_setting_ring_music, WATCH_TEXT_ID_RINGTONE_SWITCH, btn_action },
    //{ &icon_setting_network, WATCH_TEXT_ID_WIFI, btn_action },
#if USE_LV_BARCODE != 0
    { ICON_SETTING_QRCODE, WATCH_TEXT_ID_CREATE_BARCODE, barcode_create_event_cb },
#endif /*USE_LV_BARCODE*/
#if USE_LV_WATCH_DUAL_SIM != 0
    { ICON_SETTING_DUAL_SIM, WATCH_TEXT_ID_DUAL_SIM, dualsim_create_btn_action },
#endif
#if USE_LV_WATCH_MOBILE_NETWORK != 0
    { ICON_SETTING_MOBILE_NETWORK, WATCH_TEXT_ID_MOBILE_NETWORK, mobile_network_create_btn_action },
#endif
    /**only for craneg A1**/
#if defined(USE_LV_VAD) && (USE_LV_VAD > 0)
    { ICON_SETTING_WAKEUP, WATCH_TEXT_ID_ENGINEER_MODE_VOICE_WAKEUP, voice_wakeup_create_btn_event_cb },
#endif
#if USE_LV_WATCH_SEARCH_SWTICH != 0
    { ICON_SETTING_DAIL_FORMAT_SWITCH, WATCH_TEXT_ID_SEARCH_SWITCH, search_switch_create_btn_action },
#endif
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void setting_init(void)
{
    nv_watch_settings_t * nvsettings;

    // malloc memory for NVM reading buffer
    nvsettings = (nv_watch_settings_t *)lv_mem_alloc(sizeof(nv_watch_settings_t));
    memset(nvsettings, 0, sizeof(nv_watch_settings_t));

    // read NVM
    UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, 0, sizeof(nv_watch_settings_t), (UINT8 *)nvsettings);

    // call sub module init functions
    setting_brightness_init(nvsettings);
    setting_volume_init(nvsettings);
    lv_lang_set(nvsettings->currentlanguage);
#if USE_LV_WATCH_LANG_ARABIC != 0
    if(nvsettings->currentlanguage == LANG_AR) {
        language_switch_ttf(LV_ARABIC_FONT_TTF);
    }
#endif
    if(0 == nvsettings->timesetflg) {
#if USE_LV_FLASH_FS
        Hal_Rtc_Settime(NULL);

        printf("sync rtc time in setting_init\n");
#endif
    }

#if USE_LV_WATCH_DUAL_SIM
    dualsim_init(nvsettings->simid);
#endif

#if USE_LV_WATCH_SD_AUTO_MOUNT != 0
    sd_auto_mount_init(nvsettings);
#endif

#if USE_LV_WLAN != 0
    mobile_data_switch = nvsettings->mobiledataswitch;
#endif

#if USE_LV_WATCH_MOBILE_NETWORK
    mobile_network_init(nvsettings->apninfo);
#endif

#if USE_LV_WATCH_SEARCH_SWTICH
    search_switch_init(nvsettings->searchswitch);
#endif

#if USE_LV_WATCH_FLYING_MODE
    flying_mode_init(nvsettings->flyingmodeswitch);
#endif

    lv_mem_free(nvsettings);
}

lv_obj_t * setting_create(lv_obj_t * par)
{
    (void)par;

    lv_obj_t * obj = lv_watch_list_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;

    lv_watch_list_set_name_txt(obj, WATCH_TEXT_ID_SETTINGS);

    for(int8_t i = 0; i < (sizeof(settingList) / sizeof(settingList[0])); i++) {
        lv_obj_t * btn = NULL;
#if USE_NON_DIRECT_POWER_OFF
        if(settingList[i].txtId == WATCH_TEXT_ID_SHUTDOWN && watch_modem_sim_present_check_req()) {
            btn = lv_watch_list_add(obj, (void *)ICON_SETTING_FORBIDDEN_SHUTDOWN, settingList[i].txtId, NULL);
        } else {
            btn = lv_watch_list_add(obj, (void *)settingList[i].img_src, settingList[i].txtId, settingList[i].event_cb);
        }
#else
        btn = lv_watch_list_add(obj, (void *)settingList[i].img_src, settingList[i].txtId, settingList[i].event_cb);
#endif /* USE_NON_DIRECT_POWER_OFF */

        if(settingList[i].txtId == WATCH_TEXT_ID_MOBILE_NETWORK) {
            if(flying_mode_get_state() || !watch_modem_sim_present_check_req()) {
                lv_obj_set_click(btn, false);
                lv_obj_set_style_local_text_color(lv_obj_get_child(btn,NULL), LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
            }
        }
    }

    return obj;
}

void setting_create_event_cb(lv_obj_t * btn, lv_event_t e)
{
    (void)btn;
    if(LV_EVENT_CLICKED == e)
    {
        lv_watch_png_cache_all_free();
        lv_obj_t * obj = setting_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
    }
}

#if USE_LV_WLAN != 0
bool setting_get_mobile_data_switch(void)
{
    if(0 == mobile_data_switch) {
        return false;
    } else if(1 == mobile_data_switch) {
        return true;
    } else {
        printf("%s: warning, too early to get mobile data switch %d\n",
               __FUNCTION__, mobile_data_switch);
        return true;
    }
}

void setting_set_mobile_data_switch(bool on_off)
{
    printf("%s: old %d, new %d\n", __FUNCTION__, mobile_data_switch, on_off);

    if((0 == mobile_data_switch) && on_off) {
        mobile_data_switch = 1;
    } else if((1 == mobile_data_switch) && (false == on_off)) {
        mobile_data_switch = 0;
    } else {
        return;
    }
    UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_MOBILE_DATA_OFFSET,
                    sizeof(uint8_t), &mobile_data_switch);

    if(hal_wlan_is_connected_state()) {
        printf("%s: not change cm state during wifi connection\n", __FUNCTION__);
        return;
    }

    watch_modem_set_connection_switch(on_off);
}

void setting_init_mobile_data_switch(void)
{
    bool on_off = watch_modem_get_connection_switch();
    if((0 != mobile_data_switch) && (1 != mobile_data_switch)) {
        // first get mobiledataswitch value from nvm
        mobile_data_switch = (on_off) ? 1 : 0;
        UI_NV_Write_Req(NV_SECTION_UI_SETTINGS, NV_MOBILE_DATA_OFFSET,
                        sizeof(uint8_t), &mobile_data_switch);
    } else {
        bool setting_on_off = (1 == mobile_data_switch) ? true : false;
        if(on_off != setting_on_off) {
            printf("%s: recover connection manager %d\n", __FUNCTION__, setting_on_off);
            watch_modem_set_connection_switch(setting_on_off);
        }
    }

    hal_wlan_get_cm_conn_setting_cb_reg(setting_get_mobile_data_switch);
    printf("%s: mobile data switch %d, current state %d\n",
           __FUNCTION__, mobile_data_switch, on_off);
}
#endif

bool setting_is_flying_mode(void)
{
#if USE_LV_WATCH_FLYING_MODE != 0
    return flying_mode_get_state();
#else
    return false;
#endif
}

lv_obj_t * setting_get_list_btn(uint16_t text_id)
{
    lv_obj_t * btn = NULL;
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_SETTING);
    if(activity_obj) {
        lv_obj_t * watch_obj = NULL;
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
        if(watch_obj == NULL) {
            return NULL;
        }
        lv_watch_list_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        btn = lv_list_get_next_btn(ext->list, NULL);

        while(btn) {
            lv_obj_t * label = lv_obj_get_child(btn, NULL);
            if(label->user_data.user_num == text_id) {
                break;
            }
            btn = lv_list_get_next_btn(ext->list, btn);
        }
    }
    return btn;
}
/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*USE_LV_WATCH_SETTING*/
